<?php

class Manager extends Controller {
	
	function Manager()
	{
		parent::Controller();
		
		$this->load->model('collection');
		$this->load->model('frame');
		
		$this->load->helper('file');
		$this->load->helper('form');
	}
	
	function index()
	{
		redirect("rendering", 'refresh');
	}
	
	function showModification($id=0, $idCol=0)
	{
		if ($id != 0 && $idCol != 0)
		{
			$data['frame'] = $this->frame->getFrame($id);
			$data['idCol'] = $idCol;
			$data['title'] = "Modification d'une frame";
			$this->load->view('render/modif.php', $data);
		}
		else echo("erreur");
	}
	
	function showCreate($idCol=0)
	{
		if ($idCol != 0)
		{
			$data['idCol'] = $idCol;
			$data['title'] = "Creation d'une frame";
			$this->load->view('render/create.php', $data);
		}
		else echo("erreur");
	}
	
	function modifier($id=0, $idCol=0)
	{
		if ($id != 0 && $idCol != 0)
		{	
			$verif = false;
			
			$tex = $this->input->post('tex');
			
			$titre = $this->getFrameTitle($tex);
			
			$this->make($idCol, $tex, $id);
			
			//$verif = $this->verification();
			
			$verif = true;
			
			
			if ($verif == true)
				$this->frame->update($id, $titre, $tex);
			else echo("erreur dans la frame");
			
			redirect('/rendering/getFramesFromCol/'.$idCol.'#frame_'.$id, 'refresh');
		}
		else echo("erreur");
	}
	
	function make($idCol, $tex, $id)
	{
		$col = $this->collection->getCol($idCol);
		
		$input = "";
		$input .= ($col->PACKAGES);
		$input .= "\begin{document}";
		$input .= stripslashes($tex);
		$input .= "\end{document}";
		
		// recherche du fichier .tex dans l'archive décompressé
		$texFile = substr(exec('./find.sh '.$this->config->item('dirUpload').$col->DIR." '*.tex'"), 1);
		$texFilePath = $this->config->item('dirUpload').$col->DIR.$texFile;
		$pos = strrpos($texFilePath, "/");
		$texPath = substr($texFilePath, 0, $pos);
		
		// chemin d'écriture du nouveau fichier
		$name = "beamR_tmp";
		$path = $texPath."/".$name.".tex";
		// écriture du fichier
		if ( ! write_file($path, $input)) echo 'Unable to write the file';
		
		// compilation du nouveau fichier
		$this->compile($texPath, $name);
		
		exec("./clean.sh ".$texPath." ".$name);
		
		$pdfFile = exec('./find.sh '.$this->config->item('dirUpload').$col->DIR." '".$name.".pdf'");
		
		// usage = makeThumbnail path file page id
		// WARNING : file = .pdf
		// exemple : ./makeThumbnail.sh ./uploads/google/ ./Google/google.pdf 2 345
		
		//./makeThumbnail.sh ./uploads/google ./Google/google.pdf 0 2075
		
		exec("./makeThumbnail.sh ".$this->config->item('dirUpload').$col->DIR." ".$pdfFile." 0 ".$id);
	}
	
	function compile($path, $filename)
	{	
		// usage = make path filename
		// WARNING : filename = filename sans extension
		// exemple : ./make.sh ./uploads/google/ google
		
		exec("./make.sh ".$path." ".$filename);
	}
	
	// Récupération du Titre de la frame
	function getFrameTitle($frame)
	{
		preg_match_all("#frametitle{(.*?)}#si", $frame, $title, PREG_SET_ORDER);
		
		if (isset($title[0][1])) $result = $title[0][1];
		else $result = "Page sans titre";
		
		return $result;
	}
	
	function create($idCol=0)
	{
		if($idCol!=0)
		{
			$tex = $this->input->post('tex');
			
			preg_match_all("#frametitle{(.*?)}#si", $tex, $title, PREG_SET_ORDER);
			if (isset($title[0][1])) $titre = $title[0][1];
			else $titre = "Page sans titre";
			
			// insertion dans la BD de la nouvelle frame
			$id = $this->frame->insert($idCol, $titre, $tex);
			
			// creation de la miniature
			$this->make($idCol, $tex, $id);
			
			// faire vérification, et si false alors suprresion de la BD
			
			redirect('/rendering/getFramesFromCol/'.$idCol.'#frame_'.$id, 'refresh');
			
		}
		else echo("erreur");
	}
	
	function deleteFrame()
	{
		$idCol = $this->input->post('idCol');
		$frames = $this->input->post('frames');
		
		$frames = substr($frames, 0, strlen($frames)-1);
		
		$frames = split ("_", $frames);
		
		foreach($frames as $frame)
		{
			$this->frame->delete($frame, $idCol);
		}
	}
	
	function deleteCol($idCol=0)
	{
		$this->collection->delete($idCol);
		redirect('/rendering/', 'refresh');
	}
	
	
}
?>